/*
 * @Author: ChunLai
 * @Date: 2022-09-06 17:36:12
 * @LastEditTime: 2025-11-21 15:13:15
 * @Description: 日期类
 * @FilePath: \01.latest-short-insure\src\methods\date.js
 */
/**
 * 获取生日的天数
 * @param {*} sDate2 "2022-01-12"
 * @returns
 */
export const getDetailBirthDay = (sDate2) => {
  var time1 = Date.parse(new Date());
  var time2 = Date.parse(new Date(sDate2));
  var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24));
  return nDays;
};

// 出生日期--最小值
export const getBirthDate = (age, year = 1, day = 1) => {
  var today = new Date();
  var tYear = today.getFullYear() - age - year;
  var tMonth = today.getMonth();
  var tDate = today.getDate() + day;
  // console.log(tYear+"----"+tMonth)
  return new Date(tYear, tMonth, tDate);
};

// 出生日期--最大值
export const getDay = (day) => {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tDate = addStr0(tDate);
  return new Date(tYear, tMonth, tDate);
};

/**
 * 日期转字符串
 * @param {*} time 传入的时间
 */
export const dateToString = (time) => {
  const datenew = new Date(time);
  const year = datenew.getFullYear();
  const month = addStr0(datenew.getMonth() + 1);
  const date = addStr0(datenew.getDate());
  return `${year}-${month}-${date}`;
};

/**
 *
 * @param {*} startDate
 * @param {*} n n取负数表示n天/月/年前
 * @param {*} timeUnit  D"、"M"、"Y"分别表示天，月，年
 * @returns
 */

export const getAfterDay = (startDate, n, timeUnit) => {
  //year代表几年   startDate 代表开始日期
  let curr_date = new Date(startDate.replace(/-/g, "/"));
  if (timeUnit === "D") {
    curr_date.setDate(curr_date.getDate() + n);
  } else if (timeUnit === "M") {
    curr_date.setMonth(curr_date.getMonth() + n);
  } else if (timeUnit === "Y") {
    curr_date.setFullYear(curr_date.getFullYear() + n);
  }
  var strYear = curr_date.getFullYear();
  var strMonth = curr_date.getMonth() + 1;
  var strDay = curr_date.getDate();
  return `${strYear}-${addStr0(strMonth)}-${addStr0(strDay)}`;
};

// 计算出生日期+age+day 的日期
export const countYearDayForAge = (birthDate, age, day) => {
  if (!birthDate) {
    return "";
  }
  let srt = getAfterDay(birthDate, age, "Y");
  srt = getAfterDay(srt, day, "D");
  return srt;
};

export const yuLuBirthOrder = (birthDate) => {
  // console.log('yuLuBirthOrder', birthDate);
  let res = false;
  const curDate = new Date(),
    curYear = curDate.getFullYear();
  // 被保人生日
  const userMD = birthDate.slice(5),
    uerDate = `${curYear}-${userMD}`,
    endDate = `${curYear}-12-31`;
  // 投保时间次日（月日）
  const tbStamp = new Date().getTime() + 1000 * 60 * 60 * 24 * 1,
    userStamp = new Date(uerDate.replace(/-/g, "/")).getTime(),
    endStamp = new Date(endDate.replace(/-/g, "/")).getTime(),
    sxrStamp = new Date(`${curYear + 1}-01-01`.replace(/-/g, "/")).getTime(); //预录单指定生效日
  // console.log(endStamp);
  if (userStamp >= tbStamp && userStamp <= endStamp) {
    res = true;
  }
  if (userStamp == sxrStamp) {
    res = true;
  }
  return res;
};

// 生日件: 生日当天投保，保费会按照小一岁计算
export const countBirthBill = (birthDate) => {
  // console.log('yuLuBirthOrder', birthDate);
  let res = false;
  const curDate = dateToString(new Date()),
    curMD = curDate.slice(5);
  // console.log(curMD);
  // 被保人生日
  const userMD = birthDate.slice(5);
  if (curMD == userMD) {
    res = true;
  }
  return res;
};

/**
 * 起保日期
 * dateString指定日期
 */
export const getStartDate = (day, dateString = "") => {
  var today = new Date();
  if (dateString) {
    today = new Date(dateString);
  }
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  return `${tYear}-${addStr0(tMonth)}-${addStr0(tDate)}`;
};

export const getEndDate = (dateStr = "") => {
  var today = new Date();
  if (dateStr) {
    today = new Date(dateStr);
  }
  var tYear = today.getFullYear() + 1;
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  tDate = addStr0(tDate);
  if (dateStr) {
    tDate = addStr0(tDate - 1);
  }
  return `${tYear}-${addStr0(tMonth)}-${tDate}`;
};

// 日期格式化补0
export const addStr0 = (num) => String(num).padStart(2, "0");
